IF EXISTS (SELECT * FROM SYSOBJECTS WHERE ID = OBJECT_ID('HottestPaintingImport') AND sysstat & 0xf = 4)
    DROP PROCEDURE HottestPaintingImport
GO
CREATE PROCEDURE HottestPaintingImport
(
    @PaintingNumbers nvarchar(100)
)
AS
BEGIN
	SET NOCOUNT ON
	
	declare @TempPaintingNumber table(
		PaintingId int,
		PaintingNumber nvarchar(100)
	)

	insert into @TempPaintingNumber(
		PaintingId,
		PaintingNumber
	)
	select top 8 
		p.painting_id,
		tpn.Item as PaintingNumber
	from FunStringSplit(@PaintingNumbers, ',') tpn
	inner join painting p on tpn.Item=p.Painting_Number
	
	delete from hp
	from hottest_painting hp
	left join @TempPaintingNumber tpn on hp.painting_id = tpn.PaintingId
	where tpn.PaintingId is null
	
	insert into hottest_painting(
		painting_id
	)
	select 
		tpn.PaintingId
	from
		@TempPaintingNumber tpn
		left join hottest_painting hp on tpn.PaintingId = hp.painting_id
	where hp.painting_id is null
	
END
GO
